<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\common\model\SystemSet;

class Base extends Controller
{
	protected $auth;
	public $user_edit;
	
	public function __construct(){

		set_time_limit(0);
		header("Content-type:text/html;charset=utf-8");
	    parent::__construct();
	     $query = Db::name('admin_user')->where('nickname',cookie('user'))->find();
	    // echo $query['login_tz'].'---'.cookie('login_tz').'----'.cookie('user');exit;
	    if($query['login_tz']!=cookie('login_tz')){
	    	cookie('user',null);
	    	cookie('uid',null);
	    	cookie('login_tz',null);
	    }
		
		$auth = new \thinkcms\auth\Auth();
		$auth->noNeedCheckRules = ['admin/auth/role','admin/auth/menu'];
		$auth->log = true;
		$user = $auth::is_login();
		$uid_arr = array();

		/**@todo 用户编辑权限0：无，1：基本，2：高级，3：全部*/
		$this->user_edit = 0;
		//权限导航验证
		$auth_attr = Db::name("auth_role_user")->where(['user_id'=>cookie('uid')])->column('role_id');
		if(cookie('uid')!='1' && cookie('user') != 'admin' && !in_array(1,$auth_attr)){
			$role_id = Db::field('role_id')
			->table('kkty_auth_role_user')
			->where('user_id',cookie('uid'))
			->group('role_id')
			->buildSql();
			$menu_id = Db::name('auth_access')
			->alias('a')
			->join("{$role_id} b",'b.role_id=a.role_id')
			->join("__MENU__ c",'c.id=a.menu_id')
			->group('a.menu_id,c.parent_id')
			->field('a.menu_id as id,c.parent_id as parent_id,c.action')
			->select();
			$user_edit_permissions = [];
			foreach ($menu_id as $value){
				$uid_arr[] = $value['id'];
				$uid_arr[] = $value['parent_id'];
				$user_edit_permissions[] = $value['action'];
			}
			if( in_array( 'updateBaseInfo',$user_edit_permissions) && in_array('updateExtInfo',$user_edit_permissions) ){
				$this->user_edit = 3;
			}elseif( in_array( 'updateBaseInfo',$user_edit_permissions) ){
				$this->user_edit = 1;
			}elseif( in_array( 'updateExtInfo',$user_edit_permissions) ){
				$this->user_edit = 2;
			}
			$uid_arr = array_unique($uid_arr);
			$uid_arr = array_filter($uid_arr);
		}else{
			$this->user_edit = 3;
		}
		$lists = Db::name('menu')->where('parent_id',0)->select();
		$ks = input('cid');
		$list =Db::name('menu')->where('status',1)->order('list_order')->select();
		$nav = [];

		for($i=0;$i<count($list);$i++){
			$nav[$list[$i]['id']]=$list[$i];
		}
		$nav_arr = [];
		foreach ($nav as $k => $v){
			if(!empty($uid_arr)){
				if(in_array($v['id'], $uid_arr)){

					if($v['parent_id'] != 0){
						$nav[$v['parent_id']]['child'][]=&$nav[$k];
					}else{
						$nav_arr[]=&$nav[$k];
					}
				}
			}else{
				if($v['parent_id'] != 0){
					$nav[$v['parent_id']]['child'][]=&$nav[$k];
				}else{
					$nav_arr[]=&$nav[$k];
				}
			}
		}
		$tubiao = array(['icon'=>'.Hui-iconfont-user','uu'=>'xe62c'],['icon'=>'.Hui-iconfont-hongbao','uu'=>'xe6b7'],
		      			['icon'=>'.Hui-iconfont-menu','uu'=>'xe667'],['icon'=>'.Hui-iconfont-news','uu'=>'xe616'],
		      			['icon'=>'.Hui-iconfont-user-group','uu'=>'xe62b'],['icon'=>'.Hui-iconfont-order','uu'=>'xe627'],
		      			['icon'=>'.Hui-iconfont-manage2','uu'=>'xe63c']);
		$count_cz = Db::name('chongzhi_list')->where(['zt'=>0,'czadmin'=>' '])->count();
		$count_tx = Db::name('tixian_list')->where(['zt'=>0,'shadmin'=>''])->count();
		$count_dl = Db::name('username')->where(['dlshstatus'=>1,'swzh'=>['<>',1]])->count();
		$uu = Db::name('username')->where('nbzh',1)->column('username');
		$uus = implode(',', $uu);
		$balance = Db::name('agent_balance')->group("agent,FROM_UNIXTIME(add_time,'%Y-%m-%d')")->field("FROM_UNIXTIME(add_time,'%Y-%m-%d') as in_time,sum(balance) as total,agent,status")->where('balance','>',0)->where('status',0)->where('agent','not in',$uus)->order("FROM_UNIXTIME(add_time,'%Y-%m-%d') DESC")->count();
		//查询当前在线人数
//		$zaixian=Db::name('zaixian')->count();
		$zaixian=Db::name('username')->where(['current_from'=>['>',0]])->count();
		$this->assign('balance',$balance);
		$this->assign('zaixian',$zaixian);
		$this->assign('tubiao',$tubiao);
		$this->assign('czcount',$count_cz);
		$this->assign('txcount',$count_tx);
		$this->assign('dlcount',$count_dl);
		$this->assign('nav_arr',$nav_arr[0]['child']);
		$this->assign('foot',config('ADMIN_BANQUAN'));
		$this->assign('kid',$ks);
		$this->assign('lists',$lists);
		$this->assign('lis',$list);
		$this->assign('id',input('id'));
		$this->assign('cid',input('cid'));
		if($user){
			$this->uid = $user['uid'];
			$login_key = Db::name("admin_user")->where(['uid'=>$this->uid])->value("login_tz");
			if( $login_key && $login_key!=cookie('login_tz') ){
				echo '请重新登录';exit;
			}
			if(!$auth->auth()){
				echo '权限不足';exit;
			}
		}else{
			echo '请登录';exit;
		}
	}

}
